import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class P00_Panel extends JPanel  implements MouseListener, MouseMotionListener {
	
	private PanelListener listener;
	
	//1920
	private int width=0;
	//1080
	private int height=0;
	
	//menu control
	private Image[] imageMenu = new Image[9];
	private boolean[] menu_presseed = new boolean[4];
	private boolean help_pressed = false;
	
	private Image[] imageList = new Image[11];
	
	private boolean m_pressed=false;
	private boolean w_pressed=false;
	
	private boolean m_selected=false;
	private boolean w_selected=false;
	
	private boolean moving=false;
	
	private boolean warning = false;
	private boolean warning_pressed = false;
	
	public P00_Panel(int width, int height){
		
		this.width=width;
		this.height=height;
		
		//menu control
		imageMenu[0]=new ImageIcon(getClass().getResource("images/m/h0.png")).getImage();
		imageMenu[1]=new ImageIcon(getClass().getResource("images/m/h1.png")).getImage();
		imageMenu[2]=new ImageIcon(getClass().getResource("images/m/f0.png")).getImage();
		imageMenu[3]=new ImageIcon(getClass().getResource("images/m/f1.png")).getImage();
		imageMenu[4]=new ImageIcon(getClass().getResource("images/m/b0.png")).getImage();
		imageMenu[5]=new ImageIcon(getClass().getResource("images/m/b1.png")).getImage();
		imageMenu[6]=new ImageIcon(getClass().getResource("images/m/q0.png")).getImage();
		imageMenu[7]=new ImageIcon(getClass().getResource("images/m/q1.png")).getImage();
		imageMenu[8]=new ImageIcon(getClass().getResource("images/m/help/00.png")).getImage();
		
		menu_presseed[0]=false;
		menu_presseed[1]=false;
		menu_presseed[2]=false;
		menu_presseed[3]=false;
		
		
		imageList[0]=new ImageIcon(getClass().getResource("images/0/background.png")).getImage();
		imageList[1]=new ImageIcon(getClass().getResource("images/0/m0.png")).getImage();
		imageList[2]=new ImageIcon(getClass().getResource("images/0/m1.png")).getImage();
		imageList[3]=new ImageIcon(getClass().getResource("images/0/w0.png")).getImage();
		imageList[4]=new ImageIcon(getClass().getResource("images/0/w1.png")).getImage();
		imageList[5]=new ImageIcon(getClass().getResource("images/0/m2.png")).getImage();
		imageList[6]=new ImageIcon(getClass().getResource("images/0/w2.png")).getImage();
		imageList[7]=new ImageIcon(getClass().getResource("images/0/warning_background.png")).getImage();
		imageList[8]=new ImageIcon(getClass().getResource("images/0/warning_dialog.png")).getImage();
		imageList[9]=new ImageIcon(getClass().getResource("images/0/warning_b.png")).getImage();
		imageList[10]=new ImageIcon(getClass().getResource("images/0/warning_b_.png")).getImage();
		
		
		
		addMouseListener(this);
		addMouseMotionListener(this);
	  	Dimension size = new Dimension(width, height);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setLayout(null);
		
	}
	  

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		
		//draw background
		g.drawImage(imageList[0], 0, 0,null);
		
		boolean m=m_selected;
		boolean w=w_selected;
		
		if(!m && !w){
			m=true;
			w=true;
		}
		
		if(m_pressed){
			g.drawImage(imageList[5], 465-67, 350-67, null);
		}else{
			//draw man
			if(m){
				g.drawImage(imageList[1], 465, 350, null);
			}else{
				g.drawImage(imageList[2], 465, 350, null);
			}
		}
		
		if(w_pressed){
			g.drawImage(imageList[6], 1070-67, 350-67, null);
		}else{
			//draw woman
			if(w){
				g.drawImage(imageList[3], 1070, 350, null);
			}else{
				g.drawImage(imageList[4], 1070, 350, null);
			}
		}
		
		
		
		//draw menu control

		//home
		if(menu_presseed[0]){
			g.drawImage(imageMenu[1], 1800, 20, null);
		}else{
			g.drawImage(imageMenu[0], 1800, 20, null);
		}
		
		//back
		/*
		if(menu_presseed[1]){
			g.drawImage(imageMenu[5], 20, 950, null);
		}else{
			g.drawImage(imageMenu[4], 20, 950, null);
		}
		*/
		
		//forward
		if(menu_presseed[2]){
			g.drawImage(imageMenu[3], 1830, 950, null);
		}else{
			g.drawImage(imageMenu[2], 1830, 950, null);
		}
		
		//help
		if(menu_presseed[3]){
			g.drawImage(imageMenu[7], 1732, 20, null);
		}else{
			g.drawImage(imageMenu[6], 1732, 20, null);
		}
		
		//help content
		if(help_pressed){
			g.drawImage(imageMenu[8], 1365, 79, null);
		}
		
		/*
		g.drawOval(1800, 40, 78, 78);
		g.drawOval(40, 960, 75, 75);
		g.drawOval(1790, 960, 75, 75);
		
		g.drawOval(465, 350, 382, 382);
		g.drawOval(1070, 350, 382, 382);
		*/
		if(warning){
			//background
			g.drawImage(imageList[7], 0, 0,null);
			
			//dialog
			g.drawImage(imageList[8], 722, 395,null);
			
			//button
			if(!warning_pressed){
				g.drawImage(imageList[9], 889, 605,null);
			}else{
				g.drawImage(imageList[10], 889, 605,null);
			}
			
		}
		
	}
	
	public void mousePressed(MouseEvent e) {
		
		if(!warning){
			if(Math_N.distance(e.getX(),e.getY(),465+191,350+191)<=191){
	    		//m
				m_pressed=true;
	    		
	    	}
			
			if(Math_N.distance(e.getX(),e.getY(),1070+191,350+191)<=191){
	    		//w
	    		w_pressed=true;
	    	}
			
			//home
			if(1800<=e.getX() && e.getX()<=1800+63 
					&& 40<=e.getY() && e.getY()<=40+57){
				menu_presseed[0]=true;
			}
			
			//back
			/*
			if(Math_N.distance(e.getX(),e.getY(),40+75,960+75)<=75){
	    		menu_presseed[1]=true;
			}
			*/
			
			//forward
			//68 108
			if(1830<=e.getX() && e.getX()<=1830+68 
					&& 950<=e.getY() && e.getY()<=950+108){
				menu_presseed[2]=true;
			}
			
			//help
			if(1732<=e.getX() && e.getX()<=1732+60 
					&& 20<=e.getY() && e.getY()<=20+60){
				menu_presseed[3]=true;
			}
		}else{
			//warning
			if(889<=e.getX() && e.getX()<=889+142 
					&& 605<=e.getY() && e.getY()<=605+48){
				warning_pressed=true;
			}
		}
		
		
		
		
		
		this.repaint();
    }

    public void mouseReleased(MouseEvent e) {
    	
    	
    	if(menu_presseed[0] && 1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			//home
    		if(!moving){
    			listener.onHome();
        		moving=true;
    		}
		}else if(menu_presseed[1] && Math_N.distance(e.getX(),e.getY(),40+75,960+75)<=75){
			//back
			//listener.onBack();
		}else if(menu_presseed[2] && 1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
		
			//forward
			if(m_selected || w_selected){
				if(!moving){
					if(m_selected){
						listener.onChangeValue(0, 0);
					}else{
						listener.onChangeValue(0, 1);
					}
					listener.onForward();
					moving=true;
				}
			}else{
				warning=true;
			}
			
		}else if(menu_presseed[3] && 1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			help_pressed=!help_pressed;
		}
    	
    	menu_presseed[0]=false;
    	menu_presseed[1]=false;
    	menu_presseed[2]=false;
    	menu_presseed[3]=false;
    	
    	if(m_pressed && Math_N.distance(e.getX(),e.getY(),465+191,350+191)<=191){
    		//m
    		m_selected=true;
    		w_selected=false;
    		
    	}else if(w_pressed && Math_N.distance(e.getX(),e.getY(),1070+191,350+191)<=191){
    		//w
    		m_selected=false;
    		w_selected=true;
    	}
    	
    	m_pressed=false;
    	w_pressed=false;
    	
    	if(warning_pressed && 889<=e.getX() && e.getX()<=889+142 
				&& 605<=e.getY() && e.getY()<=605+48){
			warning=false;
		}
    	
    	warning_pressed=false;
    	
    	this.repaint();
    }
    
    public void mouseEntered(MouseEvent e) {
       
    }

    public void mouseExited(MouseEvent e) {
       
    }

    public void mouseClicked(MouseEvent e) {
    	
    }
    
    public void mouseMoved(MouseEvent e) {
        
    	
    }
    
    public void mouseDragged(MouseEvent e) {
        
    }
    
    public void setPanelListener(PanelListener listener){
    	this.listener=listener;
    }
}



